package com.clkj.zhonghui.service.main;

import com.clkj.zhonghui.dao.banner.BannerMapper;
import com.clkj.zhonghui.dao.order.CartMapper;
import com.clkj.zhonghui.dao.product.ProductMapper;
import com.clkj.zhonghui.dao.product.ProductSpecialsMapper;
import com.clkj.zhonghui.pojo.banner.Banner;
import com.clkj.zhonghui.pojo.order.Cart;
import com.clkj.zhonghui.pojo.product.Product;
import com.clkj.zhonghui.pojo.product.ProductComment;
import com.clkj.zhonghui.pojo.product.ProductSpecials;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @author yawang
 * @create 2024/07/16 15:45:44
 **/
@Service
public class MainServiceImpl implements MainService{
    @Autowired
    private BannerMapper bannerMapper;
    @Autowired
    private ProductSpecialsMapper productSpecialsMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CartMapper cartMapper;
    private Map map;
    @Override
    public Map listMain(Integer shopId) {
        map = new HashMap();
        Example example = new Example(Banner.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("bannerStatus",1).andIn("bannerFlag",Arrays.asList(0,4));
        List<Banner> bannerList = bannerMapper.selectByExample(example);
        map.put("bannerList",bannerList);
        example = new Example(ProductSpecials.class);
        criteria = example.createCriteria();
        criteria.andEqualTo("specialFlag",0).andEqualTo("specialStatus",1);
        example.orderBy("specialPaixu").desc();
        List<ProductSpecials> specials =productSpecialsMapper.selectByExample(example);
        for (ProductSpecials special : specials) {
            String [] ids = special.getSpecialProducts().split(",");//首页展示的商品
            example = new Example(Product.class );
            criteria = example.createCriteria();
            criteria.andIn("productId", Arrays.asList(ids));
            special.setProductList(productMapper.selectByExample(example));
        }
        map.put("specials",specials);
        return map;
    }

    @Override
    public void addCart(Cart cart) {
        Example example = new Example(Cart.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",cart.getUserId())
                .andEqualTo("productId",cart.getProductId())
                .andEqualTo("formatId",cart.getFormatId());
        Cart cart1 = cartMapper.selectOneByExample(example);
        if(cart1==null){
            cartMapper.insertSelective(cart);
        }else{
            cart1.setProductNum(cart1.getProductNum()+cart.getProductNum());
            cartMapper.updateByPrimaryKeySelective(cart1);
        }

    }

    @Override
    public List<Cart> listUserCart(Integer userId) {
        return cartMapper.listUserCart(userId);
    }

    @Override
    public void delCart(String ids) {
        String  id[] = ids.split(",");
        List<Integer> idList = new ArrayList<>();
        for (String s : id) {
            idList.add(Integer.parseInt(s));
        }
        Example example =new Example(Cart.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("cartId",idList);
        cartMapper.deleteByExample(example);
    }
}
